package com.myzl.web.controller.visualization;

import com.myzl.coal.domain.Device;
import com.myzl.coal.service.IDeviceService;
import com.myzl.coal.service.IRfidService;
import com.myzl.common.core.domain.AjaxResult;
import com.myzl.common.enums.DeviceTypeEnum;
import com.myzl.web.controller.coal.request.DetectionStatusRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cew
 * @date 2023年7月12日15:59:48
 */
@Api("可视化-设备")
@RestController
@Slf4j
@Data
@RequestMapping("/visualization/device/")
public class BigScreenDeviceController {

    private final IDeviceService deviceService;
    private final IRfidService rfidService;

    /**
     * 获取设备表详细信息
     */
    @GetMapping(value = "getCamera/{id}")
    public AjaxResult getCamera(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(deviceService.getCamera(id));
    }

    /**
     * 获取设备表详细信息
     */
    @GetMapping(value = "getDeviceInfo/{id}")
    public AjaxResult getDeviceInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(deviceService.selectDeviceById(id));
    }

    @PutMapping(value = "/config")
    public AjaxResult configDensity(@RequestBody Device device){
        return deviceService.configDensity(device);
    }

    @ApiOperation("设置筛板检测状态")
    @PutMapping(value = "/sievePlatDetectionConfig")
    public AjaxResult sievePlatDetectionConfig(@RequestBody DetectionStatusRequest detectionStatusRequest){
        return rfidService.sievePlatDetectionConfig(detectionStatusRequest.getDeviceId(),detectionStatusRequest.getDeviceNum(),detectionStatusRequest.getDetectionStatus());
    }
    @GetMapping(value = "/getAllScale")
    public AjaxResult getAllScale(){
        Device device = new Device();
        device.setDeviceType(DeviceTypeEnum.DOUBLE_ORDER.getCode());
        return AjaxResult.success(deviceService.selectDeviceList(device));
    }

    /**
     * 获取设备表详细信息
     */
    @GetMapping(value = "/getLEDInfo")
    public AjaxResult getLEDInfo(@RequestParam(defaultValue = "201") Integer deviceId)
    {
        return deviceService.getLEDDevice(deviceId);
    }

    @PutMapping(value = "/setupDeviceInfo")
    public AjaxResult setupDeviceInfo(@RequestBody List<Device> deviceList)
    {
        for (Device device : deviceList) {
            if (device.getDeviceType().equals(DeviceTypeEnum.DOUBLE_ORDER.getCode())){
                boolean flag = deviceService.validateDensity(device);
                if (flag){
                    deviceService.configDensity(device);
                }
            } else {
                deviceService.replaceConfig(device);
                deviceService.updateById(device);
                deviceService.ledConfigCache(device);
            }
        }
        return AjaxResult.success();
    }
}
